//
//  SYMomentsTableViewCell.m
//  BShouGongKe
//
//  Created by lanou on 15/8/11.
//  Copyright (c) 2015年 L750. All rights reserved.
//

#import "SYMomentsTableViewCell.h"
#import "Header.h"
#import <UIImageView+WebCache.h>

@interface SYMomentsTableViewCell ()
@property (nonatomic,assign)NSInteger imageCount;
@end


@implementation SYMomentsTableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _noneView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        _noneView.backgroundColor = [UIColor colorWithRed:247 / 255.0 green:239 / 255.0 blue:239 / 255.0 alpha:1];
        [self.contentView addSubview:_noneView];
        
        _iconView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 40, 40)];
        _iconView.layer.cornerRadius = 20;
        _iconView.layer.masksToBounds = YES;
        _iconView.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:_iconView];
        
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 20, 150, 20)];
        _nameLabel.font = [UIFont systemFontOfSize:15];
        _nameLabel.textColor = [UIColor blackColor];
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:_nameLabel];
        
        _momentsLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 50, SCREEN_WIDTH - 70, 30)];
        _momentsLabel.font = [UIFont systemFontOfSize:14];
        _momentsLabel.numberOfLines = 0;
        [self.contentView addSubview:_momentsLabel];
        
        
        _timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 130, 20, 120, 15)];
        _timeLabel.font = [UIFont systemFontOfSize:12];
        _timeLabel.textColor = [UIColor colorWithRed:150 / 255.0 green:150 / 255.0 blue:150 / 255.0 alpha:1];
        [self.contentView addSubview:_timeLabel];
        
        _view1 = [[UIImageView alloc]init];
        [self.contentView addSubview:_view1];
        
        _view2 = [[UIImageView alloc]init];
        [self.contentView addSubview:_view2];
        
        _view3 = [[UIImageView alloc]init];
        [self.contentView addSubview:_view3];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return self;
}


+ (CGFloat)heightForStr:(NSString *)str{
    CGFloat height = [str boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 70, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil].size.height;
    return height;
}


+ (CGFloat)handelImageViewWithModel:(SYMomentsFirstModel *)model{
    CGFloat height = [self heightForStr:model.text];
    NSInteger imageCount = model.img.count;
    if (imageCount != 0) {
        return 50 + height + SCREEN_WIDTH - 100;
    }else{
        return height + 60;
    }
}


- (void)setModel:(SYMomentsFirstModel *)model{
    
    _nameLabel.text = model.nick_name;
    _momentsLabel.text = model.text;
    _timeLabel.text = model.add_time;
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadImageWithURL:[NSURL URLWithString:model.face_img] options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        _iconView.image = image;
    }];
    
    //设置文字自适应高度
    CGFloat height = [model.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 70, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil].size.height;
    
    _momentsLabel.frame = CGRectMake(60, 50, SCREEN_WIDTH - 70, height);
    
    //设置图片的自适应高度
    NSInteger imageCount = model.img.count;
    if (imageCount == 0) {
        _view1.frame = CGRectZero;
        _view2.frame = CGRectZero;
        _view3.frame = CGRectZero;
        return;
    }
    if (imageCount == 1) {
        _view1.frame = CGRectMake(_momentsLabel.frame.origin.x, _momentsLabel.frame.origin.y + height + 10, SCREEN_WIDTH - 110, SCREEN_WIDTH - 120);
        [_view1 sd_setImageWithURL:[NSURL URLWithString:model.img[0]]];
        _view2.frame = CGRectZero;
        _view3.frame = CGRectZero;
        return;
    }
    if (imageCount == 2) {
        _view1.frame = CGRectMake(_momentsLabel.frame.origin.x, _momentsLabel.frame.origin.y + height + 10, (SCREEN_WIDTH - 80) / 3 * 2, SCREEN_WIDTH - 120);
        [_view1 sd_setImageWithURL:[NSURL URLWithString:model.img[0]]];
        _view2.frame = CGRectMake(_momentsLabel.frame.origin.x + _view1.frame.size.width + 5, _momentsLabel.frame.origin.y + height + 10, (SCREEN_WIDTH - 80) / 3, (SCREEN_WIDTH - 122) / 2);
        [_view2 sd_setImageWithURL:[NSURL URLWithString:model.img[1]]];
        _view3.frame = CGRectZero;
        return;
    }
    if (imageCount == 3) {
        _view1.frame = CGRectMake(_momentsLabel.frame.origin.x, _momentsLabel.frame.origin.y + height + 10, (SCREEN_WIDTH - 80) / 3 * 2, SCREEN_WIDTH - 120);
        [_view1 sd_setImageWithURL:[NSURL URLWithString:model.img[0]]];
        _view2.frame = CGRectMake(_momentsLabel.frame.origin.x + _view1.frame.size.width + 5, _momentsLabel.frame.origin.y + height + 10, (SCREEN_WIDTH - 80) / 3, (SCREEN_WIDTH - 122) / 2);
        [_view2 sd_setImageWithURL:[NSURL URLWithString:model.img[1]]];
        _view3.frame = CGRectMake(_momentsLabel.frame.origin.x + _view1.frame.size.width + 5, _view2.frame.origin.y + _view2.frame.size.height + 2, (SCREEN_WIDTH - 80) / 3, (SCREEN_WIDTH - 122) / 2);
        [_view3 sd_setImageWithURL:[NSURL URLWithString:model.img[2]]];
        return;
    }
    
}


+(CGFloat)heightForCellWithModel:(SYMomentsFirstModel *)model{
    return [self handelImageViewWithModel:model];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
